home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / tcshsrc.zoo / tcsh / tc.str.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-25  |  8.3 KB  |  408 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.00/RCS/tc.str.c,v 3.2 1991/07/19 01:06:11 christos Exp $ */
  2. /*
  3.  * tc.str.c: Short string package
  4.  *          This has been a lesson of how to write buggy code!
  5.  */
  6. /*-
  7.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  * 3. All advertising materials mentioning features or use of this software
  19.  *    must display the following acknowledgement:
  20.  *    This product includes software developed by the University of
  21.  *    California, Berkeley and its contributors.
  22.  * 4. Neither the name of the University nor the names of its contributors
  23.  *    may be used to endorse or promote products derived from this software
  24.  *    without specific prior written permission.
  25.  *
  26.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36.  * SUCH DAMAGE.
  37.  */
  38. #include "config.h"
  39. RCSID("$Id: tc.str.c,v 3.2 1991/07/19 01:06:11 christos Exp $")
  40.  
  41. #include "sh.h"
  42.  
  43. #define MALLOC_INCR    128
  44.  
  45. #ifdef SHORT_STRINGS
  46. Char  **
  47. blk2short(src)
  48.     register char **src;
  49. {
  50.     size_t     n;
  51.     register Char **sdst, **dst;
  52.  
  53.     /*
  54.      * Count
  55.      */
  56.     for (n = 0; src[n] != NULL; n++);
  57.     sdst = dst = (Char **) xmalloc((size_t) ((n + 1) * sizeof(Char *)));
  58.  
  59.     for (; *src != NULL; src++)
  60.     *dst++ = SAVE(*src);
  61.     *dst = NULL;
  62.     return (sdst);
  63. }
  64.  
  65. char  **
  66. short2blk(src)
  67.     register Char **src;
  68. {
  69.     size_t     n;
  70.     register char **sdst, **dst;
  71.  
  72.     /*
  73.      * Count
  74.      */
  75.     for (n = 0; src[n] != NULL; n++);
  76.     sdst = dst = (char **) xmalloc((size_t) ((n + 1) * sizeof(char *)));
  77.  
  78.     for (; *src != NULL; src++)
  79.     *dst++ = strsave(short2str(*src));
  80.     *dst = NULL;
  81.     return (sdst);
  82. }
  83.  
  84. Char   *
  85. str2short(src)
  86.     register char *src;
  87. {
  88.     static Char *sdst;
  89.     static size_t dstsize = 0;
  90.     register Char *dst, *edst;
  91.  
  92.     if (src == NULL)
  93.     return (NULL);
  94.  
  95.     if (sdst == (NULL)) {
  96.     dstsize = MALLOC_INCR;
  97.     sdst = (Char *) xmalloc((size_t) dstsize * sizeof(Char));
  98.     }
  99.  
  100.     dst = sdst;
  101.     edst = &dst[dstsize];
  102.     while (*src) {
  103.     *dst++ = (Char) ((unsigned char) *src++);
  104.     if (dst == edst) {
  105.         dstsize += MALLOC_INCR;
  106.         sdst = (Char *) xrealloc((ptr_t) sdst,
  107.                      (size_t) dstsize * sizeof(Char));
  108.         edst = &sdst[dstsize];
  109.         dst = &edst[-MALLOC_INCR];
  110.     }
  111.     }
  112.     *dst = 0;
  113.     return (sdst);
  114. }
  115.  
  116. char   *
  117. short2str(src)
  118.     register Char *src;
  119. {
  120.     static char *sdst = NULL;
  121.     static size_t dstsize = 0;
  122.     register char *dst, *edst;
  123.  
  124.     if (src == NULL)
  125.     return (NULL);
  126.  
  127.     if (sdst == NULL) {
  128.     dstsize = MALLOC_INCR;
  129.     sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
  130.     }
  131.     dst = sdst;
  132.     edst = &dst[dstsize];
  133.     while (*src) {
  134.     *dst++ = (char) *src++;
  135.     if (dst == edst) {
  136.         dstsize += MALLOC_INCR;
  137.         sdst = (char *) xrealloc((ptr_t) sdst,
  138.                      (size_t) dstsize * sizeof(char));
  139.         edst = &sdst[dstsize];
  140.         dst = &edst[-MALLOC_INCR];
  141.     }
  142.     }
  143.     *dst = 0;
  144.     return (sdst);
  145. }
  146.  
  147. Char   *
  148. s_strcpy(dst, src)
  149.     register Char *dst, *src;
  150. {
  151.     register Char *sdst;
  152.  
  153.     sdst = dst;
  154.     while (*dst++ = *src++);
  155.     return (sdst);
  156. }
  157.  
  158. Char   *
  159. s_strncpy(dst, src, n)
  160.     register Char *dst, *src;
  161.     register size_t n;
  162. {
  163.     register Char *sdst;
  164.  
  165.     if (n == 0)
  166.     return(dst);
  167.  
  168.     sdst = dst;
  169.     do 
  170.     if ((*dst++ = *src++) == '\0') {
  171.         while (--n != 0)
  172.         *dst++ = '\0';
  173.         return(sdst);
  174.     }
  175.     while (--n != 0);
  176.     return (sdst);
  177. }
  178.  
  179. Char   *
  180. s_strcat(dst, src)
  181.     register Char *dst, *src;
  182. {
  183.     register short *sdst;
  184.  
  185.     sdst = dst;
  186.     while (*dst++);
  187.     --dst;
  188.     while (*dst++ = *src++);
  189.     return (sdst);
  190. }
  191.  
  192. #ifdef NOTUSED
  193. Char   *
  194. s_strncat(dst, src, n)
  195.     register Char *dst, *src;
  196.     register size_t n;
  197. {
  198.     register Char *sdst;
  199.  
  200.     if (n == 0) 
  201.     return (dst);
  202.  
  203.     sdst = dst;
  204.  
  205.     while (*dst++);
  206.     --dst;
  207.  
  208.     do 
  209.     if ((*dst++ = *src++) == '\0')
  210.         return(sdst);
  211.     while (--n != 0);
  212.  
  213.     *dst = '\0';
  214.     return (sdst);
  215. }
  216.  
  217. #endif
  218.  
  219. Char   *
  220. s_strchr(str, ch)
  221.     register Char *str;
  222.     int ch;
  223. {
  224.     do
  225.     if (*str == ch)
  226.         return (str);
  227.     while (*str++);
  228.     return (NULL);
  229. }
  230.  
  231. Char   *
  232. s_strrchr(str, ch)
  233.     register Char *str;
  234.     int ch;
  235. {
  236.     register Char *rstr;
  237.  
  238.     rstr = NULL;
  239.     do
  240.     if (*str == ch)
  241.         rstr = str;
  242.     while (*str++);
  243.     return (rstr);
  244. }
  245.  
  246. size_t
  247. s_strlen(str)
  248.     register Char *str;
  249. {
  250.     register size_t n;
  251.  
  252.     for (n = 0; *str++; n++);
  253.     return (n);
  254. }
  255.  
  256. int
  257. s_strcmp(str1, str2)
  258.     register Char *str1, *str2;
  259. {
  260.     for (; *str1 && *str1 == *str2; str1++, str2++);
  261.     /*
  262.      * The following case analysis is necessary so that characters which look
  263.      * negative collate low against normal characters but high against the
  264.      * end-of-string NUL.
  265.      */
  266.     if (*str1 == '\0' && *str2 == '\0')
  267.     return (0);
  268.     else if (*str1 == '\0')
  269.     return (-1);
  270.     else if (*str2 == '\0')
  271.     return (1);
  272.     else
  273.     return (*str1 - *str2);
  274. }
  275.  
  276. int
  277. s_strncmp(str1, str2, n)
  278.     register Char *str1, *str2;
  279.     register size_t n;
  280. {
  281.     if (n == 0)
  282.     return (0);
  283.     do {
  284.     if (*str1 != *str2) {
  285.         /*
  286.          * The following case analysis is necessary so that characters 
  287.          * which look negative collate low against normal characters
  288.          * but high against the end-of-string NUL.
  289.          */
  290.         if (*str1 == '\0')
  291.         return (-1);
  292.         else if (*str2 == '\0')
  293.         return (1);
  294.         else
  295.         return (*str1 - *str2);
  296.     }
  297.         if (*str1 == '\0')
  298.         return(0);
  299.     str1++, str2++;
  300.     } while (--n != 0);
  301.     return(0);
  302. }
  303.  
  304. Char   *
  305. s_strsave(s)
  306.     register Char *s;
  307. {
  308.     Char   *n;
  309.     register Char *p;
  310.  
  311.     if (s == 0)
  312.     s = STRNULL;
  313.     for (p = s; *p++;);
  314.     n = p = (Char *) xmalloc((size_t) ((p - s) * sizeof(Char)));
  315.     while (*p++ = *s++);
  316.     return (n);
  317. }
  318.  
  319. Char   *
  320. s_strspl(cp, dp)
  321.     Char   *cp, *dp;
  322. {
  323.     Char   *ep;
  324.     register Char *p, *q;
  325.  
  326.     if (!cp)
  327.     cp = STRNULL;
  328.     if (!dp)
  329.     dp = STRNULL;
  330.     for (p = cp; *p++;);
  331.     for (q = dp; *q++;);
  332.     ep = (Char *) xmalloc((size_t)
  333.               (((p - cp) + (q - dp) - 1) * sizeof(Char)));
  334.     for (p = ep, q = cp; *p++ = *q++;);
  335.     for (p--, q = dp; *p++ = *q++;);
  336.     return (ep);
  337. }
  338.  
  339. Char   *
  340. s_strend(cp)
  341.     register Char *cp;
  342. {
  343.     if (!cp)
  344.     return (cp);
  345.     while (*cp)
  346.     cp++;
  347.     return (cp);
  348. }
  349.  
  350. Char   *
  351. s_strstr(s, t)
  352.     register Char *s, *t;
  353. {
  354.     do {
  355.     register Char *ss = s;
  356.     register Char *tt = t;
  357.  
  358.     do
  359.         if (*tt == '\0')
  360.         return (s);
  361.     while (*ss++ == *tt++);
  362.     } while (*s++ != '\0');
  363.     return (NULL);
  364. }
  365.  
  366. #endif                /* SHORT_STRINGS */
  367.  
  368. char   *
  369. short2qstr(src)
  370.     register Char *src;
  371. {
  372.     static char *sdst = NULL;
  373.     static size_t dstsize = 0;
  374.     register char *dst, *edst;
  375.  
  376.     if (src == NULL)
  377.     return (NULL);
  378.  
  379.     if (sdst == NULL) {
  380.     dstsize = MALLOC_INCR;
  381.     sdst = (char *) xmalloc((size_t) dstsize * sizeof(char));
  382.     }
  383.     dst = sdst;
  384.     edst = &dst[dstsize];
  385.     while (*src) {
  386.     if (*src & QUOTE) {
  387.         *dst++ = '\\';
  388.         if (dst == edst) {
  389.         dstsize += MALLOC_INCR;
  390.         sdst = (char *) xrealloc((ptr_t) sdst,
  391.                      (size_t) dstsize * sizeof(char));
  392.         edst = &sdst[dstsize];
  393.         dst = &edst[-MALLOC_INCR];
  394.         }
  395.     }
  396.     *dst++ = (char) *src++;
  397.     if (dst == edst) {
  398.         dstsize += MALLOC_INCR;
  399.         sdst = (char *) xrealloc((ptr_t) sdst,
  400.                      (size_t) dstsize * sizeof(char));
  401.         edst = &sdst[dstsize];
  402.         dst = &edst[-MALLOC_INCR];
  403.     }
  404.     }
  405.     *dst = 0;
  406.     return (sdst);
  407. }
  408.